package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DailyRecAdapter;
import flc.ast.adapter.RankAdapter;
import flc.ast.databinding.ActivityPopRankBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class PopRankActivity extends BaseAc<ActivityPopRankBinding> {

    private RankAdapter rankAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private int selPos;
    private List<StkResBean> listTop = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_pop_rank;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPopRankBack.setOnClickListener(v -> onBackPressed());
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.rlTop1.setOnClickListener(this);
        mDataBinding.rlTop2.setOnClickListener(this);
        mDataBinding.rlTop3.setOnClickListener(this);

        mDataBinding.rvRankList.setLayoutManager(new LinearLayoutManager(mContext));
        rankAdapter = new RankAdapter();
        mDataBinding.rvRankList.setAdapter(rankAdapter);
        rankAdapter.setOnItemClickListener(this);

        mDataBinding.refreshRankItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshRankItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshRankItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshRankItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshRankItem.finishRefresh(refreshTime);
            }
        });

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlTop1:
                if (selPos == 0) {
                    toDetail();
                    return;
                }
                selPos = 0;
                clearSelection();
                mDataBinding.ivTop1Img.setImageResource(R.drawable.ashiwangwsmxz);
                mDataBinding.ivTop1Text.setVisibility(View.VISIBLE);
                break;
            case R.id.rlTop2:
                if (selPos == 1) {
                    toDetail();
                    return;
                }
                selPos = 1;
                clearSelection();
                mDataBinding.ivTop2Img.setImageResource(R.drawable.azhenghunjiex);
                mDataBinding.ivTop2Text.setVisibility(View.VISIBLE);
                break;
            case R.id.rlTop3:
                if (selPos == 2) {
                    toDetail();
                    return;
                }
                selPos = 2;
                clearSelection();
                mDataBinding.ivTop3Img.setImageResource(R.drawable.aquanzhigaosxz);
                mDataBinding.ivTop3Text.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getData();
        getTopData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.RANK_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (page == 1) {
                        rankAdapter.setList(result);
                    } else {
                        rankAdapter.addData(result);
                    }
                }
            }
        });
    }

    private void getTopData() {
        StkResApi.getTagResourceList(this, AppConfig.TOP_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listTop.addAll(result);
                    }
                }
            }
        });
    }

    private void toDetail() {
        StkResBean bean = listTop.get(selPos);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    private void clearSelection() {
        mDataBinding.ivTop1Img.setImageResource(R.drawable.aswgwsm);
        mDataBinding.ivTop1Text.setVisibility(View.INVISIBLE);
        mDataBinding.ivTop2Img.setImageResource(R.drawable.azhenghj);
        mDataBinding.ivTop2Text.setVisibility(View.INVISIBLE);
        mDataBinding.ivTop3Img.setImageResource(R.drawable.aquanzhigs);
        mDataBinding.ivTop3Text.setVisibility(View.INVISIBLE);
    }

}